package org.west.sky.frame.activiti.study.chapter15;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.west.sky.frame.activiti.study.util.ActivitiEngineUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * author: chz
 * date: 2025/4/15
 * description: Camel任务
 */
@Slf4j
public class RunDemo15_3 {

    @Test
    public void run() {
        Properties properties = System.getProperties();
        properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.ssl.protocols", "TLSv1.2");

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("activiti.spring.xml");

        ProcessEngineConfiguration processEngineConfiguration = (ProcessEngineConfiguration) applicationContext.getBean("processEngineConfiguration");
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();

        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("processes/chapter15/Chapter15_3.bpmn20.xml").deploy();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();

        Map<String, Object> variables = new HashMap<>();
        variables.put("userName", "测试");
        variables.put("userMail", "1312817285@qq.com");
        variables.put("ip", "183.162.202.92");
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId(), variables);

        HistoryService historyService = processEngine.getHistoryService();
        historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstance.getId()).list().forEach(instance -> {
            System.out.println("====变量：" + instance.getVariableName() + "的值为：" + instance.getValue());
        });

        processEngine.close();
    }
}
